package com.ruoyi.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.blog.domain.bo.BlogContentBo;
import com.ruoyi.blog.domain.vo.BlogContentVo;
import com.ruoyi.blog.domain.BlogContent;
import com.ruoyi.blog.mapper.BlogContentMapper;
import com.ruoyi.blog.service.IBlogContentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 博客内容Service业务层处理
 *
 * @author zhutianyu
 * @date 2021-08-19
 */
@Service
public class BlogContentServiceImpl extends ServicePlusImpl<BlogContentMapper, BlogContent, BlogContentVo> implements IBlogContentService {

    @Override
    public BlogContentVo queryById(String id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<BlogContentVo> queryPageList(BlogContentBo bo) {
        PagePlus<BlogContent, BlogContentVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<BlogContentVo> queryList(BlogContentBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<BlogContent> buildQueryWrapper(BlogContentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogContent> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getContent()), BlogContent::getContent, bo.getContent());
        lqw.eq(bo.getHasDeleted() != null, BlogContent::getHasDeleted, bo.getHasDeleted());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BlogContentBo bo) {
        BlogContent add = BeanUtil.toBean(bo, BlogContent.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(BlogContentBo bo) {
        BlogContent update = BeanUtil.toBean(bo, BlogContent.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BlogContent entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
